import { MongooseModule } from '@nestjs/mongoose'
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document } from 'mongoose'
import { hashSync } from 'bcryptjs'

export type UserDocument = WxUser & Document

// @Schema({ collection: 'users' }) // 可选、指定集合/表
@Schema() // 默认转换成class.name小写负数形式  wxusers
export class WxUser {
  @Prop({ required: true })
  username: string

  @Prop({
    select: false, // 默认查询不展示
    required: true,
    set(val) {
      return hashSync(val) // 散列
    },
  })
  password: string

  @Prop()
  jobs: [string]

  @Prop({ default: 'normal' })
  role: string
}

export const UserSchema = SchemaFactory.createForClass(WxUser)

export const UserMongoModule = MongooseModule.forFeature([
  { name: WxUser.name, schema: UserSchema },
])
